package com.ruoyi.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.http.HttpUtils;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

public class WechatUtil {
    public static String getAccessToken(String appId,String appSecret) {
        String accessToken = "";
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret=" + appSecret;
        try {
            String resultString = HttpUtils.sendGet(url);
            System.out.println("获取微信accessToken:{}"+ resultString);
            if (StringUtils.isNotEmpty(resultString)) {
                JSONObject jsonObject = JSON.parseObject(resultString);
                accessToken = jsonObject.get("access_token").toString();
            } else {
                System.out.println("返回值为空，请检查请求报文或者请求地址是否正确");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return accessToken;
    }

    /**
     * 获取手机号
     */
    public static String getPhoneNumber(String code,String appId,String appSecret) {
        String phoneNumber = "";
        String url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + getAccessToken(appId,appSecret);
//        HashMap<String, String> params = new HashMap<>();
//        params.put("code", code);
        try {
            Map<String,Object> map=new LinkedHashMap<>();
            map.put("code",code);
            String resultString = HttpUtils.sendPost(url, JSONObject.toJSONString(map) );
            System.out.println("获取微信手机号码resultString==================================="+resultString);

            if (StringUtils.isNotEmpty(resultString)) {
                JSONObject jsonObject = JSON.parseObject(resultString);
                JSONObject phone_info = jsonObject.getJSONObject("phone_info");
                phoneNumber = phone_info.getString("phoneNumber");
            } else {
                System.out.println("返回值为空，请检查请求报文或者请求地址是否正确");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return phoneNumber;
    }

    public static JSONObject getSessionKeyOrOpenId(String code,String appId,String secret,String url) {
        String requestUrl = "https://api.weixin.qq.com/sns/jscode2session";
        Map<String, String> requestUrlParam = new HashMap<>();
        // https://mp.weixin.qq.com/wxopen/devprofile?action=get_profile&token=164113089&lang=zh_CN
        //小程序appId

        requestUrlParam.put("appid", appId);

        //小程序secret
        requestUrlParam.put("secret", secret);
        //小程序端返回的code
        requestUrlParam.put("js_code", code);
        //默认参数
        requestUrlParam.put("grant_type", "authorization_code");
        //发送post请求读取调用微信接口获取openid用户唯一标识
        JSONObject jsonObject = JSON.parseObject(HttpClientUtil.doPost(url, requestUrlParam));
        return jsonObject;

    }
}
